package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.GongYingShangUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.IGongYingShangUserService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 食堂档口供应商信息表 前端控制器
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-09
 */
@RestController
@RequestMapping("/system/gongYingShangUser")
public class GongYingShangUserController extends BaseController {


    @Resource
    private IGongYingShangUserService service;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GongYingShangUser user)
    {
        startPage();
        List<GongYingShangUser> list = service.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 获取用户列表,不分页
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        GongYingShangUser user = new GongYingShangUser();
        user.setStatus("0");
        user.setParams(new HashMap<>());
        user.getParams().put(TableSupport.PAGE_NUM, 1);
        user.getParams().put(TableSupport.PAGE_SIZE, 100000);

        startPage();
        List<GongYingShangUser> list = service.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId))
        {
            GongYingShangUser user = service.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, user);
        }
        return ajax;
    }

    /**
     * 新增用户
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GongYingShangUser user)
    {
        if (service.haveUserName(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && service.havePhonenumber(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        user.setCreateBy(getUsername());
        user.setCreateTime(new Date());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(service.insertUser(user));
    }


    /**
     * 修改用户
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody GongYingShangUser user)
    {
        if (!service.haveUserName(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !service.havePhonenumber(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.updateById(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(service.deleteUserByIds(userIds));
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody GongYingShangUser user)
    {
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(service.updateById(user));
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody GongYingShangUser user)
    {
        user.setUpdateBy(getUsername());
        user.setUpdateTime(new Date());
        return toAjax(service.updateById(user));
    }

}
